//鎻掍欢鍚嶅瓧锛歱ower-slider锛堝鍔熻兘婊氬姩鎻掍欢锛�
//鎻掍欢浣滆€咃細钄″疂鍧�
//浣滆€呭崥瀹�細http://caibaojian.com
//鎻掍欢缃戠珯锛歨ttp://caibaojian.com/power-slider
//浣跨敤鍗忚锛氬湪淇濈暀澶撮儴鐗堟潈鐨勬儏鍐典笅锛屼釜浜哄拰鍟嗕笟鍧囧彲鍏嶈垂浣跨敤
//浣跨敤鑼冧緥锛歨ttp://caibajian.com/demo/power-slider
;(function($) {
	$.fn.powerSlider = function(options) {
		return this.each(function() {
			var defualts = { 
				handle: "top",
				//婊氬姩鏂瑰紡
				prevNext: true,
				//鏄剧ず涓婁笅鐨勫鑸�
				Nav: true,
				//鏄剧ず鏁板瓧
				myTitle: false,
				//褰撲负鍥剧墖鏃朵笖姣忓睆涓轰竴寮犳椂鎵嶆樉绀烘爣棰�
				speed: 600,
				//鍔ㄧ敾閫熷害
				delayTime: 6000,
				//鍔ㄧ敾闂撮殧
				clickMode: "click",
				//鏁板瓧瀵艰埅婊戣繃鏂瑰紡
				sliderNum: 1
			};
			//handle 涓哄浘鐗囨粴鍔ㄦ柟寮�
			var opts = $.extend({}, defualts, options),
				obj = $(this),
				index = 0,
				sliderBox = $(".sliderbox", obj),
				sliderLi = sliderBox.find("li"),
				sliderLiWidth = $(".sliderbox li:eq(0)").outerWidth(),
				liNum = sliderLi.length,
				len = (sliderLi.length) / (opts.sliderNum),
				len = Math.ceil(len),
				sliderNav = $(" .slidernav", obj),
				sliderText = $(" .slidertext", obj),
				prev = $(" .prev", obj),
				next = $(" .next", obj),
				sliderTimer, navHtml = '',
				textHtml = '';
			//鍦ㄥ姩鐢昏繕娌℃湁寮€濮嬩箣鍓嶉瀹氫箟鐨勫唴瀹�	
			for(var i = 0; i < len; i++) {
				navHtml += '<li><a href="javascript:void(0);">' + (i + 1) + '</a></li>';
			}
			//褰撳姩鐢绘柟寮忎负娓愰殣娓愮幇鏃跺繀椤诲畾涔塩ss鐨勬牱寮忥紝鍚﹀垯鍔ㄧ敾杩囩▼浼氬嚭鐜扮┖鐧界殑瑙嗚
			if(opts.handle == 'fadeTo') {
				if(opts.sliderNum<=1){
					sliderLi.css({
						"position": "absolute",
						"left": "0",
						"top": "0"
					});
				}else{
					sliderLi.each(function(i){
						$(this).css({
							"position":"absolute",
							"left":(i%opts.sliderNum)*(sliderLi.width()),
							"top":"0"
						})
					})
				}
				var nextLen = parseInt(opts.sliderNum -1);
				sliderBox.find("li:gt("+nextLen+")").hide();
				//蹇呴』鍔犺繖鍙ss锛屽惁鍒欐笎闅愭笎鏄句細鍑虹幇涓€娈电┖鐧界殑
			}
			//濡傛灉鏈夊鑸暟瀛楃殑锛宩s鍒欏湪杩欓噷娣诲姞涓婂幓
			if(opts.Nav) {
				sliderNav.append(navHtml);
			}
			//褰撳畾涔変竴灞忕殑鍔ㄧ敾鏁扮洰澶т簬1鏃讹紝鍐呭涓哄悜宸︽诞鍔�
			if(opts.sliderNum > 1) {
				sliderLi.css("float", "left");
				
			}
			// obj.css({"width":(opts.sliderNum)*sliderLiWidth}); 鐢变簬鍐呭澶氱澶氭牱鎬э紝涓攃ss鍒楄〃涔熼渶瑕佷釜鎬у寲锛屾墍鏈変笉鍔犲叆杩欎釜鍥哄畾瀹藉害锛岃浣跨敤CSS鑷畾涔夋暣浣撶殑瀹藉害
			var slidertitle = sliderText.find("li"),
				sliderA = sliderNav.find("li");
			//褰撴湁鏂囧瓧鏍囬鏃堕鍏堟樉绀虹涓€寮狅紝鍏朵綑css宸茬粡璁剧疆闅愯棌
			slidertitle.eq(0).show();
			//榛樿绗竴寮犳坊鍔犱簡涓€涓被鍚峜urrent
			sliderA.eq(0).addClass("current"), sliderLi.eq(0).addClass("current");
			//缁撴潫鍔ㄧ敾寮€濮嬩箣鍓嶇殑鍔犺浇
			//婊氬姩鐨勪富瑕佸嚱鏁帮紝i鐨勫垵濮嬪€间负绗竴涓紝涔熷氨鏄�0锛宨ndex鏄殢鐫€i鑰屽彉鍖栫殑
			function showImg(i, index) {
				var sliderHeight = obj.height(),
					sliderWidth = obj.width();
				slidertitle.hide().eq(index).show();
				sliderA.removeClass("current").eq(index).addClass("current"), sliderLi.removeClass("current").eq(index).addClass("current");

				if(opts.handle == 'top') {
					sliderBox.filter(":not(':animated')").animate({
						"top": -sliderHeight * index
					}, opts.speed);
				} else if(opts.handle == 'left') {
					sliderLi.css("float", "left");
					sliderBox.css("width", len * sliderWidth).filter(":not(':animated')").animate({
						"left": -sliderWidth * index
					}, opts.speed);
				} else if(opts.handle == 'hide') {
					var j = index + 1;
					sliderLi.hide().slice(index * (opts.sliderNum), j * (opts.sliderNum)).show();
				} else if(opts.handle == 'fadeTo') {
					sliderLi.slice(i*(opts.sliderNum),(i+1)*(opts.sliderNum)).fadeOut(opts.speed);
					sliderLi.slice(index*(opts.sliderNum),(index+1)*(opts.sliderNum)).filter(":not(':animated')").fadeIn(opts.speed);
				} else if(opts.handle == 'slideTo') {
					sliderLi.css("z-index", '1').filter(":not(':animated')").slideUp().slice(index*(opts.sliderNum),(index+1)*(opts.sliderNum)).css("z-index", "2").slideDown(opts.speed);
				}
			}
			sliderA.bind(opts.clickMode,function(){
				index = $(this).index();
				var i = sliderA.index($(".slidernav .current:eq(0)"));
				if(index != i) {
					showImg(i, index);
				}
			}).eq(0).trigger(opts.clickMode);
			if(len <= 1) {
				prev.hide();
				next.hide();
			} else {
				if(opts.prevNext) {
					//prev
					prev.click(function() {
						var i = index;
						index -= 1;
						if(index == -1) {
							index = len - 1
						};
						showImg(i, index);
					});
					// //next
					next.click(function() {
						auto();
					});
				}
			}
			//auto fn
			function auto() {
				var i = index;
				index = (index + 1) % len;

				showImg(i, index);
			}
			//set time
			var settime;
			obj.hover(function(){
				clearInterval(settime);
			},function(){
				settime = setInterval(function(){
					auto();
				},opts.delayTime);
			}).trigger("mouseleave");
			
		});	
	};
})(jQuery);